from flask import Flask, render_template, flash, request, send_from_directory
import os
import datetime
from data_sources.gaokao2025_tool import GetInfoFromId

app = Flask(__name__, static_url_path='/static', template_folder='/templates')

app.config['SECRET_KEY'] = 'lsajfl;dsjfo[ujsdfljsda'

root_dir = r'D:\成绩统计结果'


def get_date_weekday():
    date = datetime.datetime.now().strftime('%Y年%m月%d日')
    week = datetime.datetime.weekday(datetime.datetime.now())
    week_name = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
    week_day = week_name[week]
    date_week = f'{date} {week_day}'
    return date_week


def data_update():
    date = datetime.datetime.today()
    date = date.strftime('%Y年%m月%d日 %H点%M分%S秒')
    return date


@app.route('/', methods=['GET', 'POST'])
def index():  # put application's code here
    weekday = get_date_weekday()
    update_time = data_update()
    path_file = request.files.get('path_file')
    if request.method == "GET":
        return render_template('index.html', update_time=update_time)
    else:
        pass

    return render_template('index.html')


@app.route('/get_info', methods=['GET', 'POST'])
def get_info():
    weekday = get_date_weekday()
    update_time = data_update()
    path_file = request.files.get('path_file')
    if request.method == 'GET':
        return render_template('get_info.html', update_time=update_time, weekday=weekday)
    else:
        try:
            id_info = GetInfoFromId(path_file)
            id_matched, bad_df, id_bad, show_data = id_info.get_sex_birth_age()
            bad_df_html = bad_df.to_html(index=False,
                                         classes=['table', 'table-bordered', 'table-striped'],
                                         formatters={
                                             'text-align': 'center',
                                             'color': 'red'
                                         },
                                         na_rep='')
            show_data_html = show_data.to_html(index=False, classes=['table', 'table-bordered', 'table-striped'],
                                               formatters={
                                                   'text-align': 'center'
                                               })
            return render_template('get_info.html',
                                   bad_df=bad_df,
                                   id_matched=id_matched,
                                   bad_df_html=bad_df_html,
                                   id_bad=id_bad,
                                   update_time=update_time,
                                   weekday=weekday,
                                   path_file=path_file,
                                   show_data_html=show_data_html)
        except:
            flash('请输入正确的文件名或文件格式！！！')
            return render_template('get_info.html', update_time=update_time, weekday=weekday)


@app.route('/get_info/<filename>')
def id_info_download(filename):
    return send_from_directory(root_dir, filename)


if __name__ == '__main__':
    app.run()
